var listPage = avalon.define({
	$id: "page",
	uid: "",
	receiptedPageNo: 1,
	receipted: [],
	noReceiptedData: "",
	receiptedTotal: 0,	
});

mui.ready(function() {
	mui('#form').on('tap', '.mui-content-padded', function(e){
		var _id = $(this).attr('data-id');
		mui.openWindow({
			url:'./secondAgentInfo.html?a=w&id=' + _id
		});
	})
	var uid = $("#uid").val();
	listPage.uid = uid;
	// if("" != uid) {
	// 	mui.ajax('/customer/query/dealer.json', {
	// 		data: {
	// 			'parentId': parentId
	// 		},
	// 		dataType: 'json',
	// 		success: function(data) {
	// 			if(""!=data&&null!=data){
	// 				addPage.level=data.level;
	// 				addPage.companyName=data.companyName;
	// 				addPage.email=data.email;
	// 				console.log(data);
	// 			}
	// 		}
	// 	});
	// }	
	mui.init({
		pullRefresh:{
			container:'#pullrefresh',
			down:{
				style:'circle',
				contentdown: $.i18n.prop('contentDown'),
				contentover: $.i18n.prop('contentOver'),
				contentrefresh: $.i18n.prop('contentRefresh'),
				contentnomore: $.i18n.prop('noData'),
				callback:pulldownRefresh
			},
			up:{
				auto:true,
				contentdown: $.i18n.prop('contentDown'),
				contentover: $.i18n.prop('contentOver'),
				contentrefresh: $.i18n.prop('contentRefresh'),
				contentnomore: $.i18n.prop('noData'),
				callback: pullupRefresh
			}
		}
	});
	// 上拉加载
	function pulldownRefresh(){
		var self = this;
		listPage.receiptedPageNo = 1;
		listPage.receipted = [];
		listPage.noReceiptedData = "";
		
		mui.ajax('/customer/query/dealer.json', {
			data: {
				'parentId': listPage.uid
			},
			dataType: 'json',
			success: function(data) {
				console.log(data);
				var records = data.records;
				listPage.receiptedTotal = data.totalCount;
				if(null == records || records.length == 0) {
					listPage.receipted = [];
					listPage.noReceiptedData = "<img src='../../resources/images/noDataImg.png'><p>"+$.i18n.prop('No_data')+"</p>";
				} else {
					listPage.receipted = records;
					listPage.noReceiptedData = "";
					listPage.receiptedPageNo = 2;
				}
				// mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
				self.endPulldownToRefresh(false);
				i18nReady();
			}
		});
	}
	// 下拉刷新
	function pullupRefresh(){
		mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);//参数为true代表没有更多数据了.
		var self = this;
		listPage.noReceiptedData = "";
		
		mui.ajax('/customer/query/dealer.json', {
			data: {
				"parentId": listPage.uid,
				"pageNo": listPage.receiptedPageNo,
			},
			dataType: 'json',
			success: function(data) {
				console.log(data);
				var records = data.records;
				listPage.receiptedTotal = data.totalCount;
				if((listPage.receiptedPageNo <= data.totalPage) && (null != records)) {
					listPage.receipted = listPage.receipted.concat(records);
					listPage.receiptedPageNo++;
				} else {
					mui.toast($.i18n.prop('noData'));
				}
				if(data.totalCount == 0) {
					listPage.noReceiptedData = "<img src='../../resources/images/noDataImg.png'><p>"+$.i18n.prop('No_data')+"</p>";
				} else {
					listPage.noReceiptedData = "";
				}
				// mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
				self.endPulldownToRefresh(false);
				i18nReady();
			}
		});
	}
});
